<?php

/**	
 *	object rating module
 *	
 *	module that renders an ajax rating system for objects
 *
 *	@author			pieter van peteghem <pieter@onedotonly.be>
 *
 *	@copyright		oneDot only <we@onedotonly.be>
 * 	@copyright		Copyright (c) 2010, oneDot only. All rights reserved.
 * 	@license		BSD License
 */


class ObjectRate extends MOD {


	/**
	 *	constructor
	 *	
	 *	add module vars and reference (multiple modules on 1 page are possible)	
	 *	@param		array ($vars)
	 */
	 
	function __construct( $vars ) {
		$this->_addvars( $vars );
		$this->_ref();
	}


	/**
	 * 	define output
	 *	this is the base for rendering the stars at the frontend
	 */
	 
	public function output() {
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();

		$this->addclass( $this->action );
		
		if ( $result && is_array( $result ))
			$result = '<ul>' . implode( "\n", $result ) . '</ul>';
			
		if( $result )	
			return $this->parse( $result ) . $this->render_js();
		
	}
	
	
	/**
	 * 	simple rating system for any active object
	 *	this action defines an output 
	 */
	public function filter_simple() {
		global $DB, $Object;
		
		if( $results = $DB->query_rows( "select rank from glue_social where parenttype = '" . $DB->type( $Object->type ) . "' AND 
			parentid = '" . (int) $Object->id . "' AND objtype = '" . $DB->type( 'social.rate' ) . "'" )) {
		
			foreach( $results as $result )
				$actives[] = $result[ 'rank' ];
			
			natsort( $actives );
			array_pop ( $actives );
			array_shift( $actives );
				
			if( count( $actives ) > 0 )
				$value = number_format(( array_sum( $actives ) / count( $actives )), 2 );
			
		}
		if( !$value && $this->vars->value )
	    	$value = $this->vars->value;
	    else
	    	$value = 7;		
		
		
	    for( $i = 1; $i <= $this->vars->number; $i++ ) {
	    	$class = 'rate s' . $i;
	    	if( $i <= floor( $value ))
	    		$class .= ' active';
	    
	    	$res .= '<a class="' . $class . '" href="#"></a>';
	    }
	    $res .= '<span class="stable"></span>';
	
	    $vars = array( 
	    	'type'		=> $Object->type, 
	    	'ID' 		=> $Object->id, 
	    	'number' 	=> $this->vars->number, 
	    	'personal' 	=> (( $this->vars->personal ) ? true : false ), 
	    	'divider' 	=> (( $this->vars->divider ) ? $this->vars->divider : 1 )
	    );
	    $this->js = "
	    	mod = new ObjectRate( " . json_encode( $vars ) . " );
	    	mod.rate();
	    ";
	
	    return $res;
	}
	
	
	/**
	 *	output of the rating for any active object
	 *	this action defines results of the rating
	 */
	public function filter_output() {
		global $Object, $DB;
		
		if( $results = $DB->query_rows( "select rank from glue_social where parenttype = '" . $DB->type( $Object->type ) . "' AND 
			parentid = '" . (int) $Object->id . "' AND objtype = '" . $DB->type( 'social.rate' ) . "'" )) {
		
			foreach( $results as $result )
				$res[] = $result[ 'rank' ];
				
			natsort( $res );
			array_pop ( $res );
			array_shift( $res );
					
			return number_format(( array_sum( $res ) / count( $res )), 2 );
		}
	}	
	
	
	/**
	 *	ajax rater
	 *	saves value in glue_social
	 *	@param	array	vars
	 *	@param	int		value
	 *	@return	string
	 */
	public function ajax_rate() {
		global $DB;
	
		if( $this->vars->vars->personal && !$_SESSION[ 'uid' ] ) {
			return translate( 'u bent niet ingelogd' );
			
		} else {
			$value = $this->vars->value;
			if( $this->vars->vars->divider )
				$value = $value / $this->vars->vars->divider;
			
			$this->mailto( $this->vars->vars->type, (int) $this->vars->vars->ID, $value, 196 );

			$DB->query( "insert into glue_social ( `parenttype`, `parentid`, `objtype`, `objid`, `rank`) values 
				( " . $DB->type( $this->vars->vars->type ) . ", " .  $this->vars->vars->ID . ", " . $DB->type( "social.rate" ) . ", 0, " . $value . ")", 0 );
			return translate( 'u koos voor ' ) . $value . translate( ' sterren' );
		}
	}


#	Send mail with data ( sendin/sendconf )
	private function mailto( $type, $id, $value, $module ) {
		global $DB, $Engine;
		
		$Trip		 = new OBJECT( $type, (int) $id ); 
		$User		 = new USER( $_SESSION[ 'uid' ] ); 
				
		$content 	 = "<table><tbody>";
		$content 	.= "<tr><td>Reis</td><td><a href='" . engine_get( 'url' ) . $Trip->uri() ."'>" . engine_get( 'url' ) . $Trip->uri() . "</td></tr>";
		$content 	.= "<tr><td>User</td><td>". $User->get( 'fname' ) . ' ' . $User->get( 'lname' ) ."</td></tr>";
		$content 	.= "<tr><td>Rating</td><td>". $value ."</td></tr>";
		
		$content	.= "</tbody></table>";
		
		$Letter	= new MAIL( "html" );
		$Letter->generate( $module, $content );
		$Letter->send_single(( int ) engine_get( 'administrator' ));

	}
}


